package cn.mrcode.fd.blog.buutil;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * Description: 字段校验，正则提示等等<br>
 * @author zhuqiang
 */
public class FieldCheckUtil {
    private static Map<String,String> regs = new HashMap<>();
    private static Map<String,String> infoMap = new HashMap<>();
    static {
        regs.put("account","^[a-zA-Z][0-9a-zA-Z_]{5,12}$");
        infoMap.put("account","账户规则:长度为6～12,只能以字母开头，可以由字母数字下划线组成");
        regs.put("password","^[\\w\\d!#$%()*+,-.:;<=>?@^_`{}~]{6,12}$");
        infoMap.put("password","密码规则:长度为6～12,可以由字母数字特殊字符#$%()*+,-.:;<=>?@^_`{}~组成");
        regs.put("email","^[a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$");
        infoMap.put("email","请填写有效的邮箱,邮箱是您找回密码的重要途径");
    }

    /**
     * 是否匹配
     * @param fieldName 需要校验的字段名称
     * @param value 值
     * @return 匹配：返回null,不匹配：返回错误信息
     */
    public static String isMatches(String fieldName,String value){
        boolean matches = Pattern.matches(regs.get(fieldName), value);
        if(matches){
            return null;
        }else{
            return infoMap.get(fieldName);
        }
    }

    public static void main(String[] args) {
        System.out.println(isMatches("password","126s"));
    }
}
